/*STAwZ9/hXdwQT7M4Ay4d3n8ZnQW8tJ9OC/Nt3RfFw+0=*/
import React, { Component } from 'react';
import { ajax, base, cardCache } from 'nc-lightapp-front';
import { getUserQueryHistory } from '../../../public/components/tradetypeToEmptyArea/getQueryHistory';
let { setDefData, getDefData } = cardCache;
const { NCButton } = base;
import ExtentionArea from './ExtentionArea.js';
export {
    extendArea,
    getButtons,
    getTradeName,
    getInitdata
}
function extendArea(param) {
    let self = this;
    let { json, tradeNames, buttons,checkedTradetype } = self.state;
    let { dataSource, billType, tradeType, buttonClick,updateTradeType } = param;
    
    let newbuttons = getDefData('extendArea-buttons', dataSource);
    if(newbuttons){
        buttons = newbuttons;
    }
    let config={
        buttons,
        tradeNames,
        checkedTradetype,
        updateTradeType,
        getButtons,
        buttonClick,
        pages:self,
        dataSource, billType,json
    }
    // tradeNames = getTradeName(self, dataSource, billType, tradeType);
    // tradeType = tradeNames[0].code;
    // let tradeTypeCache = getDefData('extendArea-tradeCode', dataSource);
    // if(tradeTypeCache){
    //     tradeType = tradeTypeCache;
    // }
    // let buttons = getButtons(self, dataSource, billType, tradeType);
    return (
        <ExtentionArea
            {...config}
        />
        // <div className="arapEmptyList extention-area">
        //     <div className='extention-tradetype'>
        //         <span className='extention-item-title'>{json['public-000292']}</span> {/* 国际化处理： 最近选择交易类型:*/}
        //         {
        //             tradeNames.map(tradeName => {
        //                 return <a colors="info" bordered='false' fieldid='tradeType' onClick={()=>{
        //                     buttons = getButtons(self, dataSource, billType, tradeName.code)
        //                     setDefData('extendArea-tradeCode', dataSource, tradeName.code);
        //                     this.forceUpdate();
        //                 }} style={{ marginLeft: '5px' }} >{tradeName.name}</a>
        //             })
        //         }
        //     </div>
        //     <div className='extention-button' style={{ marginTop: '10px',marginLeft:'-8px' }}>
        //         {
        //             buttons.map(button => {
        //                 if (button.makeflag) {
        //                     return <NCButton colors="info" fieldid='Add' onClick={buttonClick.bind(self, self.props, 'Add')} >{json['public-000167']}</NCButton>; /** 国际化处理： 自制 */
        //                 } else {
        //                     return <NCButton colors="info" fieldid={button.src_billtype} onClick={buttonClick.bind(self, self.props, button.src_billtype)} >{button.src_billtypename}</NCButton>;
        //                 }
        //             })
        //         }
        //     </div>
        // </div>
    )
}

function getButtons(self, dataSource, billType, tradeType) {
    let pagecode = self.props.getSearchParam('p') ? self.props.getSearchParam('p') : null;
    let appcode = self.props.getSearchParam('c') ? self.props.getSearchParam('c') : self.props.getUrlParam('c');
    let tradetype2 = getDefData('sessionTradeType', dataSource);
    if (!tradeType) {
        tradeType = tradetype2;
    }
    let buttons = [];
    // this.setstate
    ajax({
        url: '/nccloud/arap/arappub/queryallbtns.do',
        data: {
            appcode: appcode,
            pagecode: pagecode,
            tradetype: tradeType,
            billtype: billType
        },
        async: false,
        success: (res) => {
            let data = res.data;
            if (data.pullbillinfo) {
                buttons = data.pullbillinfo;
                setDefData('extendArea-buttons', dataSource, buttons);
                // getButtonsKey(button, this.Info.allButtonsKey); //获取所有按钮
            }
        }
    });
    return buttons;
}

function getTradeName(self, dataSource, billType, tradeType) {
    let pagecode = self.props.getSearchParam('p') ? self.props.getSearchParam('p') : null;
    let appcode = self.props.getSearchParam('c') ? self.props.getSearchParam('c') : self.props.getUrlParam('c');
    let cacheData = getUserQueryHistory(appcode, pagecode);
    let tradeTypes = []
    if (cacheData && cacheData.length > 0) {
        cacheData.forEach((item) => {
            tradeTypes.push(item.querycondition)
        })
    } else {
        let tradetype = getDefData('sessionTradeType', dataSource);
        if (!tradetype) {
            tradeTypes.push(tradeType);
        }else{
            tradeTypes.push(tradetype);
        }
    }
    let tradeNames = [];
    ajax({
        url: '/nccloud/arap/arappub/querytradetypes.do',
        data: {
            pk_billtype: billType,
            pk_tradetype: tradeTypes
        },
        // async: false,
        success: (res) => {
            tradeTypes.forEach((tradeType) => {
                for (let i = 0; i < res.data.length; i++) {
                    if(tradeType == res.data[i].pk_billtypecode){
                        let obj = {
                            code: res.data[i].pk_billtypecode,
                            name: res.data[i].billtypename
                        };
                        tradeNames.push(obj)
                    }
                }
            })
            // res.data.forEach((item) => {
            //     tradeNames.push(item.billtypename)
            // })
        }
    });
    return tradeNames;
}

function getInitdata(self, dataSource, billType, tradeType, state) {
    let pagecode = self.props.getSearchParam('p') ? self.props.getSearchParam('p') : null;
    let appcode = self.props.getSearchParam('c') ? self.props.getSearchParam('c') : self.props.getUrlParam('c');
    let tradeTypes = []
    let tradetype2 = getDefData('sessionTradeType', dataSource);
    if (tradetype2) {
        tradeTypes.push(tradetype2);
    }else{
        tradeTypes.push(tradeType);
    }
    ajax({
        url: '/nccloud/arap/arappub/querylistemptydata.do',
        data: {
            pk_billtype: billType,
            tradeTypes: tradeTypes,
            tradeType: tradeType,
            pagecode: pagecode,
            appcode: appcode
        },
        // async: false,
        success: (res) => {
            if(res.data){
                state.buttons = res.data.buttons;
                if(!res.data.buttons){
                    return;
                }
                setDefData('extendArea-buttons', dataSource, res.data.buttons);

                let tradeNames = [];
                let tradeTypes = res.data.billtype;
                for (let i = 0; i < tradeTypes.length; i++) {
                    let obj = {
                        code: tradeTypes[i].pk_billtypecode,
                        name: tradeTypes[i].billtypename
                    };
                    tradeNames.push(obj)
                }
                state.tradeNames = tradeNames;
                self.setState({
                    tradeNames:tradeNames,
                    buttons:res.data.buttons
                });
            }
        }
    });
}

/*STAwZ9/hXdwQT7M4Ay4d3n8ZnQW8tJ9OC/Nt3RfFw+0=*/